package com.ias.agdis_r.view.flight;

import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;

import com.ias.agdis_r.R;
import com.ias.agdis_r.bean.fight.FlightQueryValue;
import com.ias.agdis_r.manager.DataBaseManager;
import com.ias.agdis_r.util.DrawableResourceUtil;
import com.ias.agdis_r.util.S;
import com.ias.agdis_r.util.flight.AirlineCompanyUtil;

import android.annotation.SuppressLint;
import android.content.Context;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

/**
 * 航班简要信息视图
 * 
 * @author Chipor
 *
 */
public class FlightStateView extends RelativeLayout {

	private static String SERVER_DATE_FORMAT = "yyyy-MM-dd HH:mm:ss";
	private static String CLIENT_DATE_FORMAT = "HH:mm";

	private static final int DEFAULT_PADDING_V = 16;
	private static final int DEFAUTL_PADDING_TOP = 0;
	private static final int DEFAULT_PADDING_BUTTOM = 3;

	/** 进港标识 */
	public static final String ADID_A = "A";
	/** 离港标识 */
	public static final String ADID_D = "D";

	/** FTS不同值对应需要显示的文本 */
	private static final Map<String, String> FTS_MAP = new HashMap<String, String>();
	/** FPP不同值对应需要显示的文本 */
	private static final Map<String, String> FPP_MAP = new HashMap<String, String>();

	private static Context context = DataBaseManager.getInstance().getContext();
	static {
		FTS_MAP.put("X", context.getString(R.string.cancel));
		FTS_MAP.put("Y", context.getString(R.string.late));
		FTS_MAP.put("S", context.getString(R.string.normal));

		FPP_MAP.put("D", context.getString(R.string.flightD));
		FPP_MAP.put("I", context.getString(R.string.flightI));
		FPP_MAP.put("M", context.getString(R.string.flightM));
		FPP_MAP.put("R", context.getString(R.string.flightR));
	}

	private TextView FLN;
	/** 进离港 */
	private TextView AOD;
	private TextView FLO;
	private TextView ACN;
	/** 计划到达时间 */
	private TextView STO;
	private TextView FPP;
	/** 特服 */
	private TextView FTS;
	private TextView PSN;
	private TextView REG;

	private ImageView flightIcon;
	private ImageView aodIndicator;
	private ImageView ftsIndicator;

	private FlightQueryValue flightQueryValue;

	public void setFlightQueryValue(FlightQueryValue flightQueryValue) {
		this.flightQueryValue = flightQueryValue;
		initData();
	}

	public void setFLN(String FLN) {
		this.FLN.setText(FLN);
		this.flightIcon.setImageResource(AirlineCompanyUtil.getArilineCompanyIconResourceId(FLN));
	}

	public void setAOD(String AOD) {

		if (S.isEmpty(AOD)) {
			this.AOD.setText(null);
			this.aodIndicator.setImageDrawable(null);
			return;
		}

		if (AOD.equals(ADID_A)) {
			this.AOD.setText(context.getString(R.string.adid_a));
			this.aodIndicator.setImageResource(R.drawable.adida);
		} else if (AOD.equals(ADID_D)) {
			this.AOD.setText(context.getString(R.string.adid_d));
			this.aodIndicator.setImageResource(R.drawable.adidd);
		} else {
			this.AOD.setText(null);
			this.aodIndicator.setImageDrawable(null);
		}

	}

	public void setFLO(String FLO) {
		this.FLO.setText(FLO);
	}

	public void setACN(String ACN) {
		this.ACN.setText(ACN);
	}

	@SuppressLint("SimpleDateFormat")
	public void setSTO(String STO) {
		try {
			SimpleDateFormat serverDateFormat = new SimpleDateFormat(SERVER_DATE_FORMAT);
			SimpleDateFormat dateFormat = new SimpleDateFormat(CLIENT_DATE_FORMAT);
			String time = dateFormat.format(serverDateFormat.parse(STO));
			this.STO.setText(time);
		} catch (Exception e) {
			this.STO.setText(null);
		}
	}

	public void setFPP(String FPP) {
		this.FPP.setText(FPP_MAP.get(FPP));
	}

	public void setFTS(String FTS) {
		this.FTS.setText(FTS_MAP.get(FTS));
		if (!S.isEmpty(FTS)) {
			ftsIndicator.setImageResource(
					DrawableResourceUtil.getDrawableResourceIdByFieldName("ftyp_" + FTS.toLowerCase(Locale.US)));
		}

	}

	public void setPSN(String PSN) {
		this.PSN.setText(PSN);
	}

	public void setREG(String REG) {
		//如果机号有内容，显示内容，否则用空格填充
		if(S.isNotEmpty(REG)) {
			this.REG.setText(REG);
		}else{
			this.REG.setText("           ");
		}

	}

	public FlightStateView(Context context, android.view.ViewGroup.LayoutParams layoutParams,
			FlightQueryValue flightInfo) {

		super(context);
		setLayoutParams(layoutParams);
		setBackgroundResource(R.drawable.selector_list_item);

		setPadding(getDipValue(DEFAULT_PADDING_V), getDipValue(DEFAUTL_PADDING_TOP), getDipValue(DEFAULT_PADDING_V),
				getDipValue(DEFAULT_PADDING_BUTTOM));

		LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		inflater.inflate(R.layout.view_flight_state, this);

		this.flightQueryValue = flightInfo;
		initView();
		initData();
	}

	public FlightStateView(Context context, AttributeSet attrs) {
		super(context, attrs);
		LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		inflater.inflate(R.layout.view_flight_state, this);
	}

	@Override
	protected void onFinishInflate() {
		super.onFinishInflate();
		initView();
	}

	private void initData() {

		// 反射调用
		// for (Method method : FlightQueryValue.class.getDeclaredMethods()) {
		// if (method.getReturnType().getName().equals(String.class.getName()))
		// {
		// try {
		// String val = (String) method.invoke(flightQueryValue, new Object[]
		// {});
		//
		// Method m =
		// FlightStateView.class.getDeclaredMethod(method.getName().replace("get",
		// "set"),
		// String.class);
		//
		// m.invoke(this, val);
		// } catch (Exception e) {
		// // TODO Auto-generated catch block
		// }
		// }
		// }

		if (null == flightQueryValue) {
			return;
		}
		// 控件赋值
		this.setFLN(flightQueryValue.getFLN());
		this.setAOD(flightQueryValue.getAOD());
		this.setFLO(flightQueryValue.getFLO());
		this.setACN(flightQueryValue.getACN());
		this.setFPP(flightQueryValue.getFPP());
		this.setFTS(flightQueryValue.getFTS());
		this.setPSN(flightQueryValue.getPSN());
		this.setREG(flightQueryValue.getREG());

		// 三个时间择优显示
		String time = null;
		if (S.isNotEmpty(flightQueryValue.getATO())) {
			time = flightQueryValue.getATO();
		} else if (S.isNotEmpty(flightQueryValue.getETO())) {
			time = flightQueryValue.getETO();
		} else if (S.isNotEmpty(flightQueryValue.getSTO())) {
			time = flightQueryValue.getSTO();
		}
		this.setSTO(time);

		// 减小内存占用
		flightQueryValue = null;

	}

	private void initView() {
		FLN = (TextView) findViewById(R.id.selectdetail_listitem_FLNO);
		AOD = (TextView) findViewById(R.id.selectdetail_listitem_ADID);
		FLO = (TextView) findViewById(R.id.selectdetail_listitem_FLOP);
		ACN = (TextView) findViewById(R.id.selectdetail_listitem_ACN);
		STO = (TextView) findViewById(R.id.selectdetail_listitem_arrivetime);
		FPP = (TextView) findViewById(R.id.selectdetail_listitem_FLTI);
		FTS = (TextView) findViewById(R.id.selectdetail_listitem_FTYP);
		PSN = (TextView) findViewById(R.id.selectdetail_listitem_PLACECODE);
		REG = (TextView) findViewById(R.id.selectdetail_listitem_REGNUMBER);

		flightIcon = (ImageView) findViewById(R.id.defult);
		aodIndicator = (ImageView) findViewById(R.id.selectdetail_listitem_ADIDImage);
		ftsIndicator = (ImageView) findViewById(R.id.selectdetail_listitem_FTYPImage);

	}

	private int getDipValue(int len) {
		return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, len, getResources().getDisplayMetrics());
	}

}
